#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <functional>
#include "InetAddr.hpp"
#include "Log.hpp"

namespace socket_ns
{
    class Socket;
    const static int gbacklog = 8;
    using socket_sptr = std::shared_ptr<Socket>;

    enum
    {
        SOCKET_ERROR = 1,
        BIND_ERROR,
        LISTEN_ERROR,
        USAGE_ERROR
    };

    // class Socket;
    // std::unique_ptr<Socket> listensock = std::make_unique<TcpSocket>();
    // listensock->BuildListenSocket();

    // std::unique_ptr<Socket> clientsock = std::make_unique<TcpSocket>();
    // clientsock->BuildClientSocket();


    class Socket
    {
    public:
        virtual void CreatSocketOrDie() = 0;
        virtual void BindSocketOrDie(InetAddr &addr) = 0;
        virtual void ListenSocketOrDie() = 0;
        virtual socket_sptr Accept(InetAddr *addr) = 0;
        virtual bool Connetcor(InetAddr &addr) = 0;

    public:
        void BuildListenSocket(InetAddr &addr)
        {
            CreatSocketOrDie();
            BindSocketOrDie(addr);
            ListenSocketOrDie();
        }
        void BuildClientSocket(InetAddr &addr)
        {
            CreatSocketOrDie();
            Connetcor(addr);
        }
    private:
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int fd) : _socket(fd)
        {
        }
        void CreatSocketOrDie() override
        {
            _socket = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_socket < 0)
            {
                LOG(FATAL, "sockfd create error");
                exit(SOCKET_ERROR);
            }
            LOG(DEBUG, "sockd cretae success, sockfd is: %d\n", _socket);
        }

        void BindSocketOrDie(InetAddr &addr) override
        {
            // 2.bind
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(addr.PORT());
            local.sin_addr.s_addr = inet_addr(addr.IP().c_str());

            int n = bind(_socket, (struct sockaddr *)&local, sizeof(local));
            if (n < 0)
            {
                LOG(FATAL, "bind error\n");
                exit(BIND_ERROR);
            }
            LOG(DEBUG, "bind cretae success, sockfd is: %d\n", _socket);
        }

        void ListenSocketOrDie() override
        {

            int n = ::listen(_socket, gbacklog);
            if (n < 0)
            {
                LOG(FATAL, "listen error\n");
                exit(LISTEN_ERROR);
            }

            LOG(DEBUG, "listen sockd success, sockfd is: %d\n", _socket);
        }

        virtual socket_sptr Accept(InetAddr *addr) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sockfd = ::accept(_socket, (struct sockaddr *)&peer, &len);
            if (sockfd < 0)
            {
                LOG(WARNING, "accept error\n");
                return nullptr;
            }
            *addr = peer;
            socket_sptr sock = std::make_shared<TcpSocket>(_socket);
            return sock;
        }

        virtual bool Connetcor(InetAddr &addr) override
        {
            struct sockaddr_in server;
            memset(&server, 0, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_addr.s_addr = inet_addr(addr.IP().c_str());
            server.sin_port = htons(addr.PORT());
            int n = connect(_socket, (struct sockaddr *)&server, sizeof(server));
            if (n < 0)
            {
                std::cerr << "connect error" << std::endl;
                return false;
            }
            return true;
        }

    private:
        int _socket;
    };


}